﻿package Core
{	
	import Interface.ShopClasses.ShopItem;

	public class PlayerInfo 
	{
		public var UID:String;
		public var Photo:String;
		public var Name:String;
		public var Money:int;
		public var Wins:String;
		public var Loses:String;
		public var Rating:String;		
		public var Theme:int;
		public var Visualization:int;
		public var NeedMoneyBonus:Boolean;
		
		private var FEquipment:Vector.<int>;		
		private var FPlayerItems:Vector.<int>;
		
		private var FOldTime:int;
		private var FNewTime:int;
		
		public function PlayerInfo():void
		{
			UID = new String();
			Photo = new String('');
			Name = new String();
			Money = 0;
			Wins = new String('0');
			Loses = new String('0');			
			Rating = new String('1500');
			Theme = 0;
			Visualization = 0;
			NeedMoneyBonus = false;
			
			FEquipment = new Vector.<int>;
			FPlayerItems = new Vector.<int>;
		}
		
		public function Fill(AData:XML = null):void
		{
			Name = AData.player.@name;
			Money = AData.player.@money;
			Rating = AData.player.@rating;
			Settings = AData.player.@settings;		
			PlayerItems = AData.player.@items;	
			FPlayerItems;
		}
		
		public function set Settings(ASettings:String):void
		{
			var vSettings:Array = ASettings.split(';');
			Theme = int(vSettings[0]);
			Visualization = int(vSettings[1]);
			Equipment = vSettings[2];
			WinsAndLoses = vSettings[3];
			OldTime = int(vSettings[4]);
		}
		
		public function get Settings():String
		{
			var vSettings:String = new String();
			vSettings += Theme.toString() + ';';
			vSettings += Visualization.toString() + ';';
			vSettings += FEquipment.toString() + ';';
			vSettings += Wins + ',' + Loses + ';';
			vSettings += FNewTime.toString();
			return vSettings;
		}
		
		private function set PlayerItems(AItems:String):void
		{
			var vItems:Array = AItems.split(',');			
			for(var i in vItems)
			{				
				FPlayerItems.push(int(vItems[i]));
			}
		}
		
		private function set Equipment(AEquipment:String):void
		{
			var vEquipment:Array = AEquipment.split(',');
			for(var i in vEquipment)
				FEquipment.push(int(vEquipment[i]));
		}
		
		private function set WinsAndLoses(AValue:String):void
		{
			var vWinsAndLoses:Array = AValue.split(',');
			Wins = vWinsAndLoses[0];
			Loses = vWinsAndLoses[1];
		}
		
		public function get CurrentEquipment():Vector.<int>
		{
			return FEquipment;
		}
		
		public function get Items():Vector.<int>
		{
			return FPlayerItems;
		}
		
		public function UpdateItem(AItem:ShopItem):void
		{
			FEquipment[AItem.Type] = AItem.GlobalID;
			AddToUserItems(AItem.GlobalID);
		}
		
		public function AddToUserItems(AID:int):void
		{
			var vID:int = -1;
			for(var i in FPlayerItems)
			{
				if(FPlayerItems[i] == AID)
				{
					vID = AID;
					break;
				}
			}
			if(vID == -1)
				FPlayerItems.push(AID);
		}
		
		public function set OldTime(ATime:int):void
		{
			FOldTime = ATime;
		}
		
		public function set NewTime(ATime:int):void
		{
			FNewTime = ATime;			
			CompareTimes();
		}
		
		private function CompareTimes():void
		{
			var vTimeDifference:int = FNewTime - FOldTime;
			var v24Hours:int = 60 * 60 * 24;
			
			trace('New time: ' + FNewTime);
			trace('Old time: ' + FOldTime);
			trace('Time difference: ' + vTimeDifference);
			
			if(vTimeDifference > v24Hours)
			{
				Money += 30;
				NeedMoneyBonus = true;
			}
			else
			{
				FNewTime = FOldTime;
			}
		}
	}	
}
